package com.android.shop.ui;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import com.android.shop.R;
import com.android.shop.api.MyHttpClient;
import com.android.shop.common.Utils;
import com.android.shop.constant.MyConstantID;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends BaseActivity {
	private EditText mUser; // 帐号编辑框
	private EditText mPassword; // 密码编辑框
	private Button login_btn;
	String result;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	Date date = new Date();
	boolean isExit = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		mUser = (EditText) findViewById(R.id.login_user_edit);
		mPassword = (EditText) findViewById(R.id.login_passwd_edit);
		login_btn = (Button) findViewById(R.id.login_btn);
		login_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				new Thread(new SubmitThread()).start(); // 开启线程提交数据
			}
		});
	}

	class SubmitThread implements Runnable {
		@Override
		public void run() {
			String user = mUser.getText().toString();
			String pwd = mPassword.getText().toString();
			result = MyHttpClient.getSingle().Login(user, pwd);
			handlerSubmit.sendEmptyMessage(11);
		}
	}

	Handler handlerSubmit = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 11) {
				if (result!=null&&!result.equals("")) {
					if (Utils.isNumeric(result) == true) {
						String user = mUser.getText().toString();
						String expires = df.format(new Date(date.getTime() + 1
								* 24 * 60 * 60 * 1000));
						SharedPreferences.Editor sharedata = getSharedPreferences(
								MyConstantID.SP_NAME, Activity.MODE_PRIVATE)
								.edit();
						sharedata.putString("loginname", user);
						sharedata.putString("expires", expires);
						sharedata.putString("sellerid", result);
						sharedata.commit();

						Toast.makeText(getApplicationContext(), "登陆成功",
								Toast.LENGTH_SHORT).show();
						Intent intent = new Intent();
						intent.setClass(LoginActivity.this, HomePageActivity.class);
						startActivity(intent);
					} else if (result.equals("NOT EMPTY")) {
						Toast.makeText(getApplicationContext(), "用户名或密码不能为空",
								Toast.LENGTH_SHORT).show();
					} else {
						Toast.makeText(getApplicationContext(), "登陆失败",
								Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(getApplicationContext(), "网络异常,请稍后重试",
							Toast.LENGTH_SHORT).show();
				}
			}
		}
	};

	@Override
	public void finish() {
		if (isExit == false) {
			isExit = true;
			Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
			new Timer().schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			}, 2500);
		} else {
			LoginActivity.super.finish();
		}
	}

}
